package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import com.easy.bean.*;
import com.easy.service.*;
import com.easy.util.*;
import com.easy.exception.*;

@RestController
@RequestMapping("/coach")
@CrossOrigin
public class CoachController {

    @Autowired
    private CoachService coachService;



    @GetMapping
    public Result getAll() {
        return Result.success(coachService.list());
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(name = "search", required = false) String searchtext) {
         MPJLambdaWrapper<Coach> wrapper = new MPJLambdaWrapper<>();
        if (searchtext != null && !searchtext.isEmpty()) {
            wrapper.like("name", searchtext);
            wrapper.or();
            wrapper.like("phone", searchtext);
            wrapper.or();
            wrapper.like("wechat", searchtext);
            wrapper.or();
            wrapper.like("recomm", searchtext);
            wrapper.or();
            wrapper.like("photo", searchtext);
        }
        return Result.success(coachService.page(page, wrapper));
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success(coachService.getById(id));
    }

    @PostMapping
    public Result create(@RequestBody Coach entity) {
        return Result.success("数据已经存储",coachService.save(entity));
    }

    @PutMapping
    public Result update( @RequestBody Coach entity) {
        return Result.success("数据已经更新",coachService.saveOrUpdate(entity));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success("数据已经删除",coachService.removeById(id));
    }
}
